﻿using System;
using System.Collections.Generic;
using System.Text;

namespace iNet
{
	/// <summary>
	/// 提供事件相關功能。
	/// </summary>
	public static class EventUtility
	{
		#region RaiseEvent
		/// <summary>
		/// 引發事件，事件處理方法所擲出的例外將被忽略。
		/// </summary>
		/// <param name="handlerList">事件處理方法串鏈。</param>
		/// <param name="sender">引發事件的物件。</param>
		/// <param name="e">事件資料。</param>
		public static void RaiseEvent<T>(this EventHandler<T> handlerList, object sender, T e) where T : EventArgs
		{
			if (handlerList != null)
			{
				Delegate[] handlers = handlerList.GetInvocationList();
				for (int i = 0; i < handlers.Length; i++)
				{
					try
					{
						handlers[i].DynamicInvoke(sender, e);
					}
					catch
					{ }
				}
			}
		}
		/// <summary>
		/// 引發事件，事件處理方法所擲出的例外將被忽略。
		/// </summary>
		/// <param name="handlerList">事件處理方法串鏈。</param>
		/// <param name="sender">引發事件的物件。</param>
		/// <param name="e">事件資料。</param>
		public static void RaiseEvent(this EventHandler handlerList, object sender, EventArgs e)
		{
			if (handlerList != null)
			{
				Delegate[] handlers = handlerList.GetInvocationList();
				for (int i = 0; i < handlers.Length; i++)
				{
					try
					{
						handlers[i].DynamicInvoke(sender, e);
					}
					catch
					{ }
				}
			}
		}
		#endregion
	}
}